from rest_framework.generics import ListAPIView
from meiduo_admin.serializers.users import UserSerializer
from meiduo_admin.utils import PageNum
from users.models import User

class UserListView(ListAPIView):

    serializer_class = UserSerializer

    pagination_class = PageNum

    def get_queryset(self):
        # 获取前端传递的keyword值
        keyword = self.request.query_params.get('keyword')
        # 如果keyword是空字符，则说明要获取所有用户数据
        if keyword is '' or keyword is None:
            return User.objects.all()
        else:
            return User.objects.filter(username=keyword)

from rest_framework.generics import ListCreateAPIView

class UserListCreateView(ListCreateAPIView):

    serializer_class = UserSerializer

    pagination_class = PageNum

    def get_queryset(self):
        # 获取前端传递的keyword值
        keyword = self.request.query_params.get('keyword')
        # 如果keyword是空字符，则说明要获取所有用户数据
        if keyword is '' or keyword is None:
            return User.objects.all()
        else:
            return User.objects.filter(username=keyword)


